package com.frx.leetcode.packageone;

import java.util.Arrays;
import java.util.HashSet;

/**
 * Created by xurenfeng on 2019/2/14.
 *
 * Given s = "leetcode",return "leotcedo"
 * 反转字符串中的元音字符
 */
public class ReverseVowel {

    private final static  HashSet<Character> set = new HashSet<>(Arrays.asList('a','e','i','o','u',
            'A','E','I','O','U'));
    public static void main(String[] args){
        String s = "leetcode";
        String s1 = reverseVowels(s);
        System.out.println(s);
        System.out.println(s1);
        int i = 0;
    }
    public static String reverseVowels(String s){
        int i = 0, j = s.length()-1;
        char[] result = new char[s.length()];
        while (i<=j){
            char ci = s.charAt(i);
            char cj = s.charAt(j);
            // 当前指针指向的第一个不是元音字符 i++
            if(!set.contains(ci)){
                result[i] = ci;
                i++;
            } //指针末尾指向的不是元音字符 j--
            else if(!set.contains(cj)){
                result[j] = cj;
                j--;
            }else //指针开始和末尾指向的都是元音字符，交换两个字符的位置
                {
                result[i] = cj;
                i++;
                result[j] = ci;
                j--;
            }
        }
        return new String(result);
    }
}
